// Sketch to fetch data from DS18B20 samples and display on an LCD shield.
// See more details at:
// https://sites.google.com/site/bulach/arduino/temperature-lcd
// 
//
// This is loosely based on various arduino / 1-wire / DS18B20 samples, like:
// http://www.arduino.cc/playground/Learning/OneWire
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <stdio.h>

#define arraysize(x) (sizeof(x) / sizeof(x[0]))

// Remember to change these configuration.
// Max lines your LCD can display.
#define MAX_LINES_LCD 2
// Ensure these are the pins used by the LCD shield.
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Ensure this is the pin connected to your DS18B20 bus.
OneWire oneWireBus(2);

DallasTemperature sensors(&oneWireBus);

struct NamedDevice {
  const char* name;
  DeviceAddress address;
};

// Ensure these represent your sensor ids, as obtained by 
// printOneWireAddressesOnce.
static NamedDevice namedDevices[] = {
  {"Inside",  { 0x28, 0x44, 0xA5, 0xAA, 0x03, 0x00, 0x00, 0x00 }},
  {"Outside", { 0x28, 0x79, 0x94, 0xAA, 0x03, 0x00, 0x00, 0xEE }}
};

void printDeviceAddress(const DeviceAddress& address, int index) {
  Serial.print("D");
  Serial.print(index);
  Serial.print("=");
  for (int i = 0; i < arraysize(address); ++i) {
    Serial.print(address[i], HEX);
    Serial.print(",");
  }
}

void printOneWireAddressesOnce() {
  static bool hasPrinted = false;
  if (hasPrinted) {
    return;
  }
  hasPrinted = true;
  int deviceCount = sensors.getDeviceCount();
  for (int i = 0; i < deviceCount; ++i) {
    DeviceAddress address = {0};
    sensors.getAddress(address, i);
    printDeviceAddress(address, i);
    Serial.print(sensors.getTempC(address));
    Serial.println();
  }  
}

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("-");
  // Initialize the sensors and set the desired resolution (12 bits).
  sensors.begin();
  printOneWireAddressesOnce();
  for (int i = 0; i < arraysize(namedDevices); ++i)
    sensors.setResolution(namedDevices[i].address, TEMP_12_BIT);
}

void loop() {
  sensors.requestTemperatures();
  for (int i = 0; i < arraysize(namedDevices); ++i) {
    char animChar = '\0';
    if (i == 0) {
      static byte counter = 0;
      static char slashes[] = {'\\', '|', '/', '-'};
      if (++counter >= arraysize(slashes)) {
        counter = 0;
      }
      animChar = slashes[counter];
    }
    float tempC = sensors.getTempC(namedDevices[i].address);
    char buf[80] = {0};
    int tempInt = tempC * 100;
    int absTempInt = tempInt < 0 ? -tempInt : tempInt;
    int remainder = absTempInt - 100 * (absTempInt / 100);
    if (i < MAX_LINES_LCD) {
      sprintf(buf, "%s %d.%d %c", namedDevices[i].name, tempInt / 100, remainder, animChar);
      lcd.setCursor(0, i);
      lcd.print(buf);
    }
    printDeviceAddress(namedDevices[i].address, i);
    sprintf(buf, "%s,%d.%d", namedDevices[i].name, tempInt / 100, remainder, animChar);
    Serial.println(buf);
  }
  delay(2500);
}

